using System;
using System.IO;
using NMock2;
using NMock2.Monitoring;

namespace NMockEvents.Test
{
    public class MockEvent
    {
        private EventHandler handler;

        public void Initialize(EventHandler handler)
        {
            this.handler = handler;
        }

        public void Raise()
        {
            handler.Invoke(null, null);
        }

        public static IAction Hookup(MockEvent mockEvent)
        {
            return new MockEventHookup(mockEvent);
        }
    }

    public class MockEventHookup : IAction
    {
        private readonly MockEvent mockEvent;
        public MockEventHookup(MockEvent mockEvent)
        {
            this.mockEvent = mockEvent;
        }

        public void Invoke(Invocation invocation)
        {
            EventHandler handler = invocation.Parameters[0] as EventHandler;
            if (handler == null)
                throw new Exception("Unknown event handler type.");
            mockEvent.Initialize(handler);
        }

        public void DescribeTo(TextWriter writer)
        {
            // do nothing
        }
    }
}